


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > AnimatedSprite</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.sprite</a>
</div>

<h1>Coverage Summary for Class: AnimatedSprite (nl.tudelft.jpacman.sprite)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">AnimatedSprite</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/11)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/38)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.sprite;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import java.awt.Graphics;
<i class="no-highlight">4</i>&nbsp;
<i class="no-highlight">5</i>&nbsp;/**
<i class="no-highlight">6</i>&nbsp; * Animated sprite, renders the frame depending on the time of requesting the
<i class="no-highlight">7</i>&nbsp; * draw.
<i class="no-highlight">8</i>&nbsp; *
<i class="no-highlight">9</i>&nbsp; * @author Jeroen Roosen 
<i class="no-highlight">10</i>&nbsp; */
<b class="nc"><i class="no-highlight">11</i>&nbsp;public class AnimatedSprite implements Sprite {</b>
<i class="no-highlight">12</i>&nbsp;
<i class="no-highlight">13</i>&nbsp;    /**
<i class="no-highlight">14</i>&nbsp;     * Static empty sprite to serve as the end of a non-looping sprite.
<i class="no-highlight">15</i>&nbsp;     */
<b class="nc"><i class="no-highlight">16</i>&nbsp;    private static final Sprite END_OF_LOOP = new EmptySprite();</b>
<i class="no-highlight">17</i>&nbsp;
<i class="no-highlight">18</i>&nbsp;    /**
<i class="no-highlight">19</i>&nbsp;     * The animation itself, in frames.
<i class="no-highlight">20</i>&nbsp;     */
<i class="no-highlight">21</i>&nbsp;    private final Sprite[] animationFrames;
<i class="no-highlight">22</i>&nbsp;
<i class="no-highlight">23</i>&nbsp;    /**
<i class="no-highlight">24</i>&nbsp;     * The delay between frames.
<i class="no-highlight">25</i>&nbsp;     */
<i class="no-highlight">26</i>&nbsp;    private final int animationDelay;
<i class="no-highlight">27</i>&nbsp;
<i class="no-highlight">28</i>&nbsp;    /**
<i class="no-highlight">29</i>&nbsp;     * Whether is animation should be looping or not.
<i class="no-highlight">30</i>&nbsp;     */
<i class="no-highlight">31</i>&nbsp;    private final boolean looping;
<i class="no-highlight">32</i>&nbsp;
<i class="no-highlight">33</i>&nbsp;    /**
<i class="no-highlight">34</i>&nbsp;     * The index of the current frame.
<i class="no-highlight">35</i>&nbsp;     */
<i class="no-highlight">36</i>&nbsp;    private int current;
<i class="no-highlight">37</i>&nbsp;
<i class="no-highlight">38</i>&nbsp;    /**
<i class="no-highlight">39</i>&nbsp;     * Whether this sprite is currently animating or not.
<i class="no-highlight">40</i>&nbsp;     */
<i class="no-highlight">41</i>&nbsp;    private boolean animating;
<i class="no-highlight">42</i>&nbsp;
<i class="no-highlight">43</i>&nbsp;    /**
<i class="no-highlight">44</i>&nbsp;     * The {@link System#currentTimeMillis()} stamp of the last update.
<i class="no-highlight">45</i>&nbsp;     */
<i class="no-highlight">46</i>&nbsp;    private long lastUpdate;
<i class="no-highlight">47</i>&nbsp;
<i class="no-highlight">48</i>&nbsp;    /**
<i class="no-highlight">49</i>&nbsp;     * Creates a new animating sprite that will change frames every interval. By
<i class="no-highlight">50</i>&nbsp;     * default the sprite is not animating.
<i class="no-highlight">51</i>&nbsp;     *
<i class="no-highlight">52</i>&nbsp;     * @param frames
<i class="no-highlight">53</i>&nbsp;     *            The frames of this animation.
<i class="no-highlight">54</i>&nbsp;     * @param delay
<i class="no-highlight">55</i>&nbsp;     *            The delay between frames.
<i class="no-highlight">56</i>&nbsp;     * @param loop
<i class="no-highlight">57</i>&nbsp;     *            Whether or not this sprite should be looping.
<i class="no-highlight">58</i>&nbsp;     */
<i class="no-highlight">59</i>&nbsp;    public AnimatedSprite(Sprite[] frames, int delay, boolean loop) {
<b class="nc"><i class="no-highlight">60</i>&nbsp;        this(frames, delay, loop, false);</b>
<i class="no-highlight">61</i>&nbsp;    }
<i class="no-highlight">62</i>&nbsp;
<i class="no-highlight">63</i>&nbsp;    /**
<i class="no-highlight">64</i>&nbsp;     * Creates a new animating sprite that will change frames every interval.
<i class="no-highlight">65</i>&nbsp;     *
<i class="no-highlight">66</i>&nbsp;     * @param frames
<i class="no-highlight">67</i>&nbsp;     *            The frames of this animation.
<i class="no-highlight">68</i>&nbsp;     * @param delay
<i class="no-highlight">69</i>&nbsp;     *            The delay between frames.
<i class="no-highlight">70</i>&nbsp;     * @param loop
<i class="no-highlight">71</i>&nbsp;     *            Whether or not this sprite should be looping.
<i class="no-highlight">72</i>&nbsp;     * @param isAnimating
<i class="no-highlight">73</i>&nbsp;     *            Whether or not this sprite is animating from the start.
<i class="no-highlight">74</i>&nbsp;     */
<b class="nc"><i class="no-highlight">75</i>&nbsp;    public AnimatedSprite(Sprite[] frames, int delay, boolean loop, boolean isAnimating) {</b>
<b class="nc"><i class="no-highlight">76</i>&nbsp;        assert frames.length &gt; 0;</b>
<i class="no-highlight">77</i>&nbsp;
<b class="nc"><i class="no-highlight">78</i>&nbsp;        this.animationFrames = frames.clone();</b>
<b class="nc"><i class="no-highlight">79</i>&nbsp;        this.animationDelay = delay;</b>
<b class="nc"><i class="no-highlight">80</i>&nbsp;        this.looping = loop;</b>
<b class="nc"><i class="no-highlight">81</i>&nbsp;        this.animating = isAnimating;</b>
<i class="no-highlight">82</i>&nbsp;
<b class="nc"><i class="no-highlight">83</i>&nbsp;        this.current = 0;</b>
<b class="nc"><i class="no-highlight">84</i>&nbsp;        this.lastUpdate = System.currentTimeMillis();</b>
<i class="no-highlight">85</i>&nbsp;    }
<i class="no-highlight">86</i>&nbsp;
<i class="no-highlight">87</i>&nbsp;    /**
<i class="no-highlight">88</i>&nbsp;     * @return The frame of the current index.
<i class="no-highlight">89</i>&nbsp;     */
<i class="no-highlight">90</i>&nbsp;    private Sprite currentSprite() {
<b class="nc"><i class="no-highlight">91</i>&nbsp;        Sprite result = END_OF_LOOP;</b>
<b class="nc"><i class="no-highlight">92</i>&nbsp;        if (current &lt; animationFrames.length) {</b>
<b class="nc"><i class="no-highlight">93</i>&nbsp;            result = animationFrames[current];</b>
<i class="no-highlight">94</i>&nbsp;        }
<b class="nc"><i class="no-highlight">95</i>&nbsp;        assert result != null;</b>
<b class="nc"><i class="no-highlight">96</i>&nbsp;        return result;</b>
<i class="no-highlight">97</i>&nbsp;    }
<i class="no-highlight">98</i>&nbsp;
<i class="no-highlight">99</i>&nbsp;    /**
<i class="no-highlight">100</i>&nbsp;     * Starts or stops the animation of this sprite.
<i class="no-highlight">101</i>&nbsp;     *
<i class="no-highlight">102</i>&nbsp;     * @param isAnimating
<i class="no-highlight">103</i>&nbsp;     *            &lt;code&gt;true&lt;/code&gt; to animate this sprite or &lt;code&gt;false&lt;/code&gt;
<i class="no-highlight">104</i>&nbsp;     *            to stop animating this sprite.
<i class="no-highlight">105</i>&nbsp;     */
<i class="no-highlight">106</i>&nbsp;    public void setAnimating(boolean isAnimating) {
<b class="nc"><i class="no-highlight">107</i>&nbsp;        this.animating = isAnimating;</b>
<i class="no-highlight">108</i>&nbsp;    }
<i class="no-highlight">109</i>&nbsp;
<i class="no-highlight">110</i>&nbsp;    /**
<i class="no-highlight">111</i>&nbsp;     * (Re)starts the current animation.
<i class="no-highlight">112</i>&nbsp;     */
<i class="no-highlight">113</i>&nbsp;    public void restart() {
<b class="nc"><i class="no-highlight">114</i>&nbsp;        this.current = 0;</b>
<b class="nc"><i class="no-highlight">115</i>&nbsp;        this.lastUpdate = System.currentTimeMillis();</b>
<b class="nc"><i class="no-highlight">116</i>&nbsp;        setAnimating(true);</b>
<i class="no-highlight">117</i>&nbsp;    }
<i class="no-highlight">118</i>&nbsp;
<i class="no-highlight">119</i>&nbsp;    @Override
<i class="no-highlight">120</i>&nbsp;    public void draw(Graphics graphics, int x, int y, int width, int height) {
<b class="nc"><i class="no-highlight">121</i>&nbsp;        update();</b>
<b class="nc"><i class="no-highlight">122</i>&nbsp;        currentSprite().draw(graphics, x, y, width, height);</b>
<i class="no-highlight">123</i>&nbsp;    }
<i class="no-highlight">124</i>&nbsp;
<i class="no-highlight">125</i>&nbsp;    @Override
<i class="no-highlight">126</i>&nbsp;    public Sprite split(int x, int y, int width, int height) {
<b class="nc"><i class="no-highlight">127</i>&nbsp;        update();</b>
<b class="nc"><i class="no-highlight">128</i>&nbsp;        return currentSprite().split(x, y, width, height);</b>
<i class="no-highlight">129</i>&nbsp;    }
<i class="no-highlight">130</i>&nbsp;
<i class="no-highlight">131</i>&nbsp;    /**
<i class="no-highlight">132</i>&nbsp;     * Updates the current frame index depending on the current system time.
<i class="no-highlight">133</i>&nbsp;     */
<i class="no-highlight">134</i>&nbsp;    private void update() {
<b class="nc"><i class="no-highlight">135</i>&nbsp;        long now = System.currentTimeMillis();</b>
<b class="nc"><i class="no-highlight">136</i>&nbsp;        if (animating) {</b>
<b class="nc"><i class="no-highlight">137</i>&nbsp;            while (lastUpdate &lt; now) {</b>
<b class="nc"><i class="no-highlight">138</i>&nbsp;                lastUpdate += animationDelay;</b>
<b class="nc"><i class="no-highlight">139</i>&nbsp;                current++;</b>
<b class="nc"><i class="no-highlight">140</i>&nbsp;                if (looping) {</b>
<b class="nc"><i class="no-highlight">141</i>&nbsp;                    current %= animationFrames.length;</b>
<b class="nc"><i class="no-highlight">142</i>&nbsp;                } else if (current == animationFrames.length) {</b>
<b class="nc"><i class="no-highlight">143</i>&nbsp;                    animating = false;</b>
<i class="no-highlight">144</i>&nbsp;                }
<i class="no-highlight">145</i>&nbsp;            }
<i class="no-highlight">146</i>&nbsp;        } else {
<b class="nc"><i class="no-highlight">147</i>&nbsp;            lastUpdate = now;</b>
<i class="no-highlight">148</i>&nbsp;        }
<i class="no-highlight">149</i>&nbsp;    }
<i class="no-highlight">150</i>&nbsp;
<i class="no-highlight">151</i>&nbsp;    @Override
<i class="no-highlight">152</i>&nbsp;    public int getWidth() {
<b class="nc"><i class="no-highlight">153</i>&nbsp;        assert currentSprite() != null;</b>
<b class="nc"><i class="no-highlight">154</i>&nbsp;        return currentSprite().getWidth();</b>
<i class="no-highlight">155</i>&nbsp;    }
<i class="no-highlight">156</i>&nbsp;
<i class="no-highlight">157</i>&nbsp;    @Override
<i class="no-highlight">158</i>&nbsp;    public int getHeight() {
<b class="nc"><i class="no-highlight">159</i>&nbsp;        assert currentSprite() != null;</b>
<b class="nc"><i class="no-highlight">160</i>&nbsp;        return currentSprite().getHeight();</b>
<i class="no-highlight">161</i>&nbsp;    }
<i class="no-highlight">162</i>&nbsp;
<i class="no-highlight">163</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
